<?php

namespace app\wxapi\controller;

use Endroid\QrCode\QrCode;
use think\Response;
use think\Session;
use think\Cookie;
use EasyWeChat\Payment\Order as WxOrder;

class User extends Api
{
    public function getIndex()
    {
        if (model('Users')->loginByOpenId($this->open_id)) {
            $user = model('Users')->getLoginUserInfo();
            return view('index', ['user' => $user]);
        }else {
            return view('login', ['auth_id' => cookie('auth_id')]);
        }       
    }
    public function getTest()
    {
        $url =$this->app->url;
        $a = $url->shorten('http://www.baidu.com');
        halt($a);
        $a=$ticket;
        return view('test',['a'=>$a]);
        $a = 'user_group';
        $tag = $this->app->$a;
        //$tag1 = $tag->create('1');
        halt($this->app->qrcode);
        halt($this->app->user_tag);
    }
    /**
     * [getCallback description]
     * @return [type] [description]
     */
    public function getCallback()
    { 
        $oauth = $this->app->oauth;
        $user = $oauth->user();
        Cookie::forever('auth_id', $user->getId());
        $this->redirect('/user/index');
    }
    /**
     * 登录
     * @return [type] [description]
     */
    public function getLogin()
    {
        if (model('Users')->login(input('mobile'), input('password'))) {
            return json(['code' => 100]);
        }else{
            return json(['code' => 200, 'msg' => model('Users')->getError()]);
        }
    }
    /**
     * 退出登录
     * @return [type] [description]
     */
    public function getLogout()
    {
        model('Users')->logout();
        Cookie::delete('auth_id');
        return view('login', ['auth_id' => cookie('auth_id')]);
    }
    public function getRecharge()
    {
        $js = $this->app->js;
        return view('recharge', ['config' => $js->config(array('chooseWXPay'))]);
    }
    /**
     * 充值
     * @return [type] [description]
     */
    public function getDoRecharge()
    {
        $intergrateadd_info['intergrate'] = model('Intergrates')->moneyToIntegral($this->request->get('total'));
        $intergrateadd_info['cash']       = $this->request->get('total') * 100;
        $intergrateadd_info['order_num']  = model('Orders')->createOrderId();
        $intergrateadd_info['user_id']    = self::getLoginUserInfo('id');
        $intergrateadd_info['status']     = 0;//halt($intergrateadd_info);

        $order = model('IntergrateOrders')->createIntergrateOrder($intergrateadd_info);
        $attributes = [
            'attach'           => 'recharge',
            'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
            'body'             => '双辉盛业积分充值订单',
            'detail'           => '双辉盛业积分充值:' . $order->intergrate,
            'out_trade_no'     => $order->order_num,
            'total_fee'        => $this->request->get('total') * 1,
            'notify_url'       => 'http://wx.shsytour.cn/wxpay/notify.html', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => $this->open_id,
        ];

        $payment = $this->app->payment;
        $js      = $this->app->js;
        $order   = new WxOrder($attributes);

        $result = $payment->prepare($order);

        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
            $p        = $payment->configForJSSDKPayment($prepayId);
            return json(['code' => 100, 'data' => $p]);
        }
        return json(['code' => 200, 'msg' => '']);
    }
    public function getIntergrate()
    {
        return $this->request->get('intergrate');
    }

    public function getWish()
    {
        $js = $this->app->js;
        return view('wish', ['config' => $js->config(array('chooseWXPay'))]);
    }

    /**
     * 地区插件
     */
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();
        return json($country);
        exit;
    }
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
}
